﻿package net.jmp0.toast.entities.obstacles 
{
	import net.jmp0.toast.entities.bases.GameElement;
	import net.jmp0.toast.entities.obstacles.Obstacle;
	import net.jmp0.toast.entities.player.Player;
	
	/**
	 * A banana
	 * @author Thodd
	 */
	public class Banana extends Obstacle
	{
		[Embed(source = '../../res/banana_left_down.png')] private var imgBananaLeftDown:Class;
		[Embed(source = '../../res/banana_left_up.png')] private var imgBananaLeftUp:Class;
		[Embed(source = '../../res/banana_right_down.png')] private var imgBananaRightDown:Class;
		[Embed(source = '../../res/banana_right_up.png')] private var imgBananaRightUp:Class;
		
		//Directionvariables
		//These indicate the directions in which the player will be redirected
		private var xDir:int = 0;
		private var yDir:int = 0;
		
		
		public function Banana(x:int, y:int, xDir:int, yDir:int) 
		{
			super(x, y);
			this.xDir = xDir;
			this.yDir = yDir;
			
			var imgSprite:Class = chooseSprite();
			
			//Standard setup stuff
			this.sprite = FP.getSprite(imgSprite, 21, 21, false, false, 1, 1, true);
			this.randomizeAnimation();	
		}
		
		private function chooseSprite():Class {
			
			var spr:Class = imgBananaLeftDown;
			
			if (xDir == -1 && yDir == -1) {
				spr = imgBananaLeftUp;
				
			}else if (xDir == 1 && yDir == -1) {
				spr =  imgBananaRightUp;
				
			}else if (xDir == 1 && yDir == 1) {
				spr =  imgBananaRightDown;
				
			}else if (xDir == -1 && yDir == 1) {
				spr =  imgBananaLeftDown;
				
			}
				
			return spr;
			
		}
		
		public function getXDir():int {
			return this.xDir;
		}
				
		public function getYDir():int {
			return this.yDir;
		}
		
		
		override public function performCollision(colPartner:GameElement):void {
			//Ok colPartner Code is quite long... but i couldn't figure out a shorter way
			//the precise collision checking is unavoidable
			
			trace("i hit a banana, xDir = " + this.getXDir() + ", yDir = " + this.getYDir());

			if (this.getXDir() == -1 && this.getYDir() == -1) {			//Banana facing Left-Up
				if(colPartner.oldXspeed >= 0 && colPartner.oldYspeed == 0){				//The player colliding from the right
					colPartner.xSpeed = 0;
					colPartner.ySpeed = -1 * colPartner.movingSpeed;
				}else if (colPartner.oldXspeed == 0 && colPartner.oldYspeed >= 0) {		//The player colliding from above
					colPartner.xSpeed = -1 * colPartner.movingSpeed;
					colPartner.ySpeed = 0;
				}
				
			}else if (this.getXDir() == -1 && this.getYDir() == 1) { 		//Banana facing Left-Down
				if(colPartner.oldXspeed >= 0 && colPartner.oldYspeed == 0){				//The player colliding from the right
					colPartner.xSpeed = 0;
					colPartner.ySpeed = 1 * colPartner.movingSpeed;
				}else if(colPartner.oldXspeed == 0 && colPartner.oldYspeed <= 0){		//The player colliding from below
					colPartner.xSpeed = -1 * colPartner.movingSpeed;
					colPartner.ySpeed = 0
				}
				
			}else if (this.getXDir() == 1 && this.getYDir() == -1) { 		//Banana facing Right-Up
				if(colPartner.oldXspeed <= 0 && colPartner.oldYspeed == 0){				//The player colliding from the left
					colPartner.xSpeed = 0;
					colPartner.ySpeed = -1 * colPartner.movingSpeed;
				}else if(colPartner.oldXspeed == 0 && colPartner.oldYspeed >= 0){		//The player colliding from above
					colPartner.xSpeed = 1 * colPartner.movingSpeed;
					colPartner.ySpeed = 0;
				}
				
			}else if (this.getXDir() == 1 && this.getYDir() == 1) { 		//Banana facing Right-Down
				if(colPartner.oldXspeed <= 0 && colPartner.oldYspeed == 0){				//The player colliding from the left
					colPartner.xSpeed = 0;
					colPartner.ySpeed = 1 * colPartner.movingSpeed;
				}else if(colPartner.oldXspeed == 0 && colPartner.oldYspeed <= 0){		//The player colliding from below
					colPartner.xSpeed = 1 * colPartner.movingSpeed;
					colPartner.ySpeed = 0;
				}
				
			}
			
			if (colPartner.xSpeed != 0 || colPartner.ySpeed != 0) {
				colPartner.x = this.x;
				colPartner.y = this.y;
				colPartner.lastCollision = this; //colPartner is important! It prevents double collision performing
			}
			
		}
		
	}

}